<%@page import="com.google.appengine.api.datastore.Query.CompositeFilter"%>
<%@page import="java.util.List"%>
<%@page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@page import="com.google.appengine.api.datastore.Query.SortDirection"%>
<%@page import="com.google.appengine.api.datastore.Query.CompositeFilterOperator"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="com.google.appengine.api.datastore.DatastoreServiceFactory"  %>
<%@page import="com.google.appengine.api.datastore.DatastoreService"  %>
<%@page import="com.google.appengine.api.datastore.Query.Filter"  %>
<%@page import="com.google.appengine.api.datastore.Query.FilterPredicate"  %>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"  %>
<%@page import="com.google.appengine.api.datastore.Query"  %>
<%@page import="com.google.appengine.api.datastore.PreparedQuery"  %>
<%@page import="com.google.appengine.api.datastore.Entity"  %>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>gAutoArd | Chart</title>

    <!-- Bootstrap core CSS -->
    <link href="/css/bootstrap.min.css" rel="stylesheet">

    <!-- gAutoArd core CSS -->
    <link href="/css/main.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="/">gAutoArd</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="https://code.google.com/p/gautoard/wiki/DesignConcepts">Help</a></li>
          </ul>
        </div>
      </div>
    </div>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li><a href="/">Overview</a></li>
            <li class="divider"></li>
            <li class="nav-header">Devices</li>
            <li><a href="/thermostat.jsp">Thermostat</a></li>
            <li><a href="/lights.jsp">Lights</a></li>
            <li><a href="/alarm.jsp">Alarm</a></li>
            <li class="active"><a href="/charts/temperature.jsp">Weekly Temperature Chart</a></li>
            <li><a href="/charts/temperature_archived.jsp">Archive Temperature Chart</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1>Cloud Temperature</h1>
          <div id='chart_div' style='width: 800px; height: 400px;'></div>
        </div>
      </div>
    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="/js/bootstrap.min.js"></script>

    <script type='text/javascript' src='http://www.google.com/jsapi'></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {'packages': ['annotatedtimeline']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
          var options = {
              title: 'Cloud Temperature',
              colors: ['blue', 'goldenrod'],
              scaleType: 'maximized',
              thickness: 2
          };
          var data = new google.visualization.DataTable();
          data.addColumn('datetime', 'Datetime');
          data.addColumn('number', 'Downstairs Temp');
          data.addColumn('string', 'title1');
          data.addColumn('string', 'text1');
          data.addColumn('number', 'Outside Temp');
          data.addColumn('string', 'title1');
          data.addColumn('string', 'text1');
          data.addRows([
      <%
          // Get the Datastore Service
          DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

          Filter outsideTemp =
                  new FilterPredicate("key",
                  FilterOperator.EQUAL,
                  "com.rasdesign.dras.home.temperature.outside");

          Filter downstairsTemp =
                  new FilterPredicate("key",
                  FilterOperator.EQUAL,
                  "com.rasdesign.dras.home.temperature.downstairs");

          Filter compfil = CompositeFilterOperator.or(outsideTemp, downstairsTemp);
          Query qot = new Query("log").setFilter(compfil).addSort("time", SortDirection.ASCENDING);
          PreparedQuery pqot = datastore.prepare(qot);
          String outsidetemp="", downstairstemp="";
          for (Entity result : pqot.asIterable(FetchOptions.Builder.withChunkSize(1000))) {
              Date datetime = (Date) result.getProperty("time");
              String key = (String) result.getProperty("key");
              if ("com.rasdesign.dras.home.temperature.outside" .equals(key)) {
                  outsidetemp = (String)result.getProperty("value");
              }
              if ("com.rasdesign.dras.home.temperature.downstairs".equals(key)) {
                  downstairstemp = (String)result.getProperty("value");
              }
              if (!"".equals(downstairstemp) && !"".equals(outsideTemp)) {
                  out.println("[new Date(" + datetime.getTime() + "), " + downstairstemp + ", undefined, undefined, "
                      + outsidetemp + ", undefined, undefined],");
              }
          }
      %>
          ]);

          var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
          chart.draw(data, options);
      }
    </script>
  </body>
</html>
